//
//  GYComposeBottomView.swift
//  Weibo
//
//  Created by vane on 16/8/17.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
enum GYComposeBottomViewType: Int {
    //图片
    case Picture = 0
    // @
    case Mention = 1
    // #
    case Trend = 2
    // 笑脸
    case Emoticon = 3
    // +
    case Add = 4
}
class GYComposeBottomView: UIView {
    
    var closure:((type: GYComposeBottomViewType)->())?
    
    var emoticonBtn: UIButton?
    
    var isEmoticon: Bool = false{
        didSet{
           
            let imgName: String
            
            if isEmoticon {
                
                imgName = "compose_keyboardbutton_background"
                
            }else {
            
                imgName = "compose_emoticonbutton_background"
            }
            
      
            emoticonBtn?.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
            
            emoticonBtn?.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        
        let pictureBtn = addChildButtons("compose_toolbar_picture",type: .Picture)
        let mentionBtn = addChildButtons("compose_mentionbutton_background",type: .Mention)
        let trendBtn = addChildButtons("compose_trendbutton_background",type: .Trend)
        emoticonBtn = addChildButtons("compose_emoticonbutton_background",type: .Emoticon)
        let addBtn = addChildButtons("compose_add_background",type: .Add)
        
        pictureBtn.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionBtn)
        }
        mentionBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(trendBtn)
            make.leading.equalTo(pictureBtn.snp_trailing)
        }
        trendBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(emoticonBtn!)
            make.leading.equalTo(mentionBtn.snp_trailing)
        }
        emoticonBtn!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.width.equalTo(addBtn)
            make.leading.equalTo(trendBtn.snp_trailing)
        }
        addBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonBtn!.snp_trailing)
        }
        
        
    }
    private func addChildButtons(imgName:String,type:GYComposeBottomViewType) ->UIButton{

        let btn = UIButton()
        
        btn.tag = type.rawValue
        
        btn.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        addSubview(btn)
        
        return btn
    }
    @objc private func btnClick(btn:UIButton){
        
         closure?(type: GYComposeBottomViewType(rawValue: btn.tag)!)
    }
}
